require 'selenium'

module LWA
    class Selenium

        private_class_method :new
        private :initialize
        @@instance = nil

        def initialize
            server_host = 'localhost'
            server_port = 4444
            browser_start_command = "*iexplore" #*firefox firefox iehta
            browser_url = 'http://ynliu:8080/'
            time_out = 30000
            @selenium = Selenium::SeleniumDriver.new(server_host, server_port, browser_start_command, browser_url, time_out)
        end

        def Selenium.create
            @@instance = new unless @@instance
            @@instance
        end

        def start
            @selenium.start
        end

        def stop
            @selenium.stop
        end

        def wait_for_page_to_load
            @selenium.wait_for_page_to_load(60000)
        end

        def open_URL(url)
            @selenium.open(url)
        end

        def selenium_get_xpath_count(xpath)
            @selenium.get_xpath_count(xpath)
        end

        def selenium_get_text(locator)
            @selenium.get_text(locator)
        end

        def selenium_get_value(textbox_id)
            @selenium.get_value(textbox_id)
        end

        def selenium_wait_for_condition(java_script, timeout)
            @selenium.wait_for_condition(java_script, timeout)
        end

        def selenium_wait_for_pop_up(window_id, timeout)
            @selenium.wait_for_pop_up(window_id, timeout)
        end

        def selenium_get_all_window_title(window_number)
            @selenium.get_all_window_titles[window_number]
        end

        def selenium_get_eval(java_script)
            @selenium.get_eval(java_script)
        end

        def selenium_open_window(url, window_id)
            @selenium.open_window(url, window_id)
        end

        def selenium_click(object_id)
            @selenium.click(object_id)
        end

        def selenium_capture_screenshot(file_name)
            @selenium.capture_screenshot(file_name)
        end

        def selenium_drag_and_drop(obj1_id, obj2_id)
            @selenium.drag_and_drop_to_object(obj1_id, obj2_id)
        end

        def is_visible(control_id)
            @selenium.is_visible(control_id)
        end

        def is_text_present_on_page(text_pattern)
            @selenium.is_text_present(text_pattern)
        end

        def is_confirmation_present
            @selenium.is_confirmation_present
        end

        def get_confirmation
            @selenium.get_confirmation
        end

        def get_value_from_checkbox(checkbox_id)
            @selenium.get_value(checkbox_id)
        end

        def get_current_selection_from_dropdown(dropdown_id)
            @selenium.get_selected_label(dropdown_id)
        end

        def set_value_to_textbox(textbox_id, value)
            @selenium.type(textbox_id, value)
        end

        def set_value_to_checkbox(checkbox_id, value)
            current = @selenium.get_value(checkbox_id)
            if (value == "on" && current == "on") || (value == "off" && current == "off")
                return
            end
            @selenium.click(checkbox_id)
        end

        def set_value_to_dropdown(dropdown_id, item)
            @selenium.select(dropdown_id, "label=#{item}")
        end

        def set_value_to_iframe(iframe_id, value)
            @selenium.wait_for_condition("selenium.browserbot.getCurrentWindow().document.getElementById('#{iframe_id}').document.getElementsByTagName('table').length > 0", 5000)
            @selenium.select_frame("//iframe[@id='#{iframe_id}']")
            @selenium.select_frame("//iframe")
            @selenium.type("//BODY", value)
            @selenium.select_frame("relative=top")
        end

        def run_script(script)
            @selenium.run_script(script)
        end
    end
end
